package com.huawei.appgallery.forum.cards.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build.VERSION;
import android.text.Layout.Alignment;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.StaticLayout;
import android.text.StaticLayout.Builder;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.TypefaceSpan;
import android.util.AttributeSet;
import android.widget.TextView;
import com.huawei.appgallery.foundation.ui.css.CSSRule;
import com.huawei.appgallery.foundation.ui.css.CSSViewProxy;
import com.huawei.appgallery.foundation.ui.css.RenderListener;
import com.huawei.appgallery.foundation.ui.css.adapter.CSSPropertyName;
import com.huawei.appgallery.foundation.ui.css.adapter.type.CSSMonoColor;
import com.huawei.appgallery.foundation.ui.css.adapter.type.CSSValue;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import o.azb;
import o.bqx;
import o.bqy;
import o.bsy.a;
import o.bsy.d;
import o.bsy.e;
import o.buc;
import o.bug;
import o.dof;
import o.erq;
import o.evf;

public class PostTitleTextView extends TextView implements RenderListener {
    private SpannableStringBuilder ʻ;
    private boolean ʼ;
    private String ʽ;
    private int ˊ;
    private int ˊॱ;
    private boolean ˋ = true;
    private bug ˎ;
    private String ˏ = "";
    private int ˏॱ;
    private StaticLayout ͺ;
    private List<String> ॱ;
    private boolean ॱˊ = false;
    private int ॱॱ = -1;
    private String ᐝ;

    public PostTitleTextView(Context context) {
        super(context);
    }

    public PostTitleTextView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    public int ˏ() {
        if (this.ॱॱ <= 0) {
            this.ॱॱ = azb.ॱ(bqy.ˋ().ˏ()) - (bqy.ˋ().ˏ().getResources().getDimensionPixelSize(e.ᐝॱ) * 2);
        }
        return this.ॱॱ;
    }

    public void setTextViewWidth(int i) {
        this.ॱॱ = i;
    }

    private void ˊ() {
        if (VERSION.SDK_INT >= 23) {
            Builder alignment = Builder.obtain(this.ˏ, 0, this.ˏ.length(), getPaint(), ˋ()).setBreakStrategy(getBreakStrategy()).setLineSpacing(getLineSpacingExtra(), getLineSpacingMultiplier()).setIncludePad(false).setAlignment(Alignment.ALIGN_NORMAL);
            if (getMaxLines() == 1) {
                alignment.setMaxLines(getMaxLines());
                alignment.setEllipsize(getEllipsize());
            }
            this.ͺ = alignment.build();
            return;
        }
        this.ͺ = new StaticLayout(this.ˏ, getPaint(), ˋ(), Alignment.ALIGN_NORMAL, getLineSpacingMultiplier(), getLineSpacingExtra(), false);
    }

    private int ˋ() {
        return (ˏ() - getPaddingLeft()) - getPaddingRight();
    }

    public void setData(String str, List<String> list) {
        if (str == null) {
            this.ˏ = "";
        } else {
            this.ˏ = str;
        }
        this.ॱ = list;
        this.ˋ = true;
        this.ˎ = null;
        ʼ();
        ˊ();
        if (this.ˎ == null) {
            setText(this.ˏ);
            ˎ();
            return;
        }
        ˋ(ʻ());
        ॱॱ();
    }

    private void ˎ() {
        if (!dof.ॱ(this.ʽ)) {
            this.ʻ = new SpannableStringBuilder(this.ˏ);
            ॱ();
            setText(this.ʻ);
        }
    }

    private void ʼ() {
        if (this.ॱ != null && this.ॱ.size() > 0) {
            this.ˎ = new bug(bqy.ˋ().ˏ());
            this.ˎ.ॱ(this.ॱˊ, this.ˊॱ);
            this.ˎ.ˋ(this.ॱ);
            this.ˎ.draw(new Canvas());
        }
    }

    private void ॱॱ() {
        CharSequence spannableStringBuilder = new SpannableStringBuilder();
        spannableStringBuilder.append(this.ˏ);
        if (!TextUtils.isEmpty(this.ʽ)) {
            this.ʻ = spannableStringBuilder;
            ॱ();
            spannableStringBuilder = this.ʻ;
        }
        if (this.ˋ) {
            spannableStringBuilder.append(ॱ(getResources().getDrawable(a.ͺ)));
        }
        spannableStringBuilder.append(ˋ(this.ˎ));
        setText(spannableStringBuilder);
    }

    private SpannableString ˋ(Drawable drawable) {
        float dimension;
        boolean z;
        SpannableString spannableString = new SpannableString("easy");
        float intrinsicWidth = (float) drawable.getIntrinsicWidth();
        if (this.ˋ) {
            dimension = getResources().getDimension(e.ˈ);
        } else {
            dimension = getResources().getDimension(e.ˊᐝ);
        }
        drawable.setBounds(0, 0, (int) intrinsicWidth, (int) dimension);
        buc o_buc = new buc(drawable);
        if (this.ˋ) {
            z = false;
        } else {
            z = true;
        }
        o_buc.ॱ(z);
        spannableString.setSpan(o_buc, 0, "easy".length(), 17);
        return spannableString;
    }

    private SpannableString ॱ(Drawable drawable) {
        SpannableString spannableString = new SpannableString("easy");
        drawable.setBounds(0, 0, (int) getResources().getDimension(e.ॱˎ), (int) getResources().getDimension(e.ˈ));
        spannableString.setSpan(new buc(drawable), 0, "easy".length(), 17);
        return spannableString;
    }

    private void ˋ(String str) {
        if (str != null) {
            TextPaint textPaint = new TextPaint(getPaint());
            int measureText = (int) textPaint.measureText(str);
            int ˋ = ˋ();
            int ʽ = (int) ʽ();
            if (ʽ <= ˋ - measureText) {
                return;
            }
            if (this.ͺ.getLineCount() != getMaxLines()) {
                this.ˏ += "\n";
                this.ˋ = false;
                return;
            }
            measureText = str.length();
            while (measureText > 0) {
                String str2 = str.substring(0, measureText) + "...";
                if (((int) textPaint.measureText(str2)) + ʽ > ˋ) {
                    measureText--;
                } else if (getMaxLines() == 1) {
                    this.ˏ = str2;
                    return;
                } else {
                    this.ˏ = this.ˏ.substring(0, this.ͺ.getLineEnd(0)) + str2;
                    return;
                }
            }
        }
    }

    private float ʽ() {
        if (this.ˋ) {
            return this.ˎ.ॱ() + getResources().getDimension(e.ॱˎ);
        }
        return this.ˎ.ॱ();
    }

    private String ʻ() {
        int lineCount = this.ͺ.getLineCount();
        if (lineCount == 0) {
            return null;
        }
        int maxLines = getMaxLines();
        if (lineCount <= maxLines || maxLines == -1) {
            maxLines = lineCount;
            lineCount = 0;
        } else {
            lineCount = 1;
        }
        maxLines--;
        int lineEnd = this.ͺ.getLineEnd(maxLines);
        if (maxLines - 1 >= 0) {
            maxLines = this.ͺ.getLineEnd(maxLines - 1);
        } else {
            maxLines = 0;
        }
        String substring = this.ˏ.substring(maxLines, lineEnd);
        if (lineCount != 0) {
            this.ˏ = this.ˏ.substring(0, lineEnd);
            ˊ();
        }
        return substring;
    }

    public void setTextHighLightColor(String str) {
        this.ˊ = Color.parseColor(str);
    }

    public void setTextHighLightColor(int i) {
        this.ˊ = getResources().getColor(i);
    }

    public void setTextHighLightTypeface(String str) {
        this.ᐝ = str;
    }

    public void setTextToHighLight(String str) {
        this.ʽ = str;
    }

    public void setCaseInSensitive(boolean z) {
        this.ʼ = z;
    }

    public void ॱ() {
        if (!dof.ॱ(this.ˏ)) {
            ᐝ();
        }
    }

    private void ᐝ() {
        int indexOf;
        if (this.ˏ.contains("...")) {
            indexOf = this.ˏ.indexOf("...");
        } else {
            indexOf = this.ˏ.length() - this.ͺ.getEllipsisCount(this.ͺ.getLineCount() - 1);
        }
        if (this.ˊ != 0 || dof.ॱ(this.ᐝ)) {
            try {
                ॱ(this.ˏ, indexOf);
            } catch (Exception e) {
                bqx.ˋ("HotTopicTitleTextView", "high light error");
            }
        }
    }

    private void ॱ(String str, int i) {
        Pattern compile;
        if (this.ʼ) {
            compile = Pattern.compile(Pattern.quote(this.ʽ), 2);
        } else {
            compile = Pattern.compile(Pattern.quote(this.ʽ));
        }
        Matcher matcher = compile.matcher(str);
        while (matcher.find()) {
            if (i == -1 || matcher.end() <= i) {
                if (this.ˊ != 0) {
                    this.ʻ.setSpan(new ForegroundColorSpan(this.ˊ), matcher.start(), matcher.end(), 33);
                }
                if (!dof.ॱ(this.ᐝ)) {
                    this.ʻ.setSpan(new TypefaceSpan(this.ᐝ), matcher.start(), matcher.end(), 33);
                }
            } else {
                return;
            }
        }
    }

    public boolean onRenderReady(CSSViewProxy cSSViewProxy) {
        CSSRule rule = cSSViewProxy.getRule();
        if (rule.getStyleDeclaration() == null) {
            return false;
        }
        CSSValue propertyValue = rule.getStyleDeclaration().getPropertyValue(CSSPropertyName.FONT_COLOR);
        if (!(propertyValue instanceof CSSMonoColor)) {
            return false;
        }
        int color = ((CSSMonoColor) propertyValue).getColor();
        this.ॱˊ = true;
        this.ˊॱ = color;
        this.ˏॱ = erq.ˊ(color, 0.5f);
        return true;
    }

    public void setUnReadTextColor() {
        if (this.ॱˊ) {
            setTextColor(this.ˊॱ);
        } else {
            setTextColor(evf.ॱ(getContext(), d.ˊ));
        }
    }

    public void setReadedTextColor() {
        if (this.ॱˊ) {
            setTextColor(this.ˏॱ);
        } else {
            setTextColor(evf.ॱ(getContext(), d.ˋ));
        }
    }

    public void setUnReadTextColor(int i) {
        setTextColor(i);
    }

    public void setReadedTextColor(int i) {
        setTextColor(i);
    }
}
